<?php
/**
 * 数据调用接口
 */
class controller_get extends controller_base {

	private $box_id = '';
	
	private $callback = '';
	
	private $output = '';
	
	/**
	 * 登录状态
	 */
	public function action_login_status() {
		$model_auth = new model_auth();
		return $this->output = $model_auth->access() ? '已登录[<a href="./?m=auth&a=logout">退出</a>]' : '未登录';
	}
	
	/**
	 * 记事数
	 */
	public function action_notes_count() {
		$model_note = new model_note();
		$today = strtotime(date('Y-m-d'));
		
		$res_today = $model_note->find("create_time >= $today", '', '', 'COUNT(*) count');
		$res_all = $model_note->find('1', '', '', 'COUNT(*) count');

		return $this->output = $res_all['count'].'-'.$res_today['count'];
	}

	/**
	 * 待办记事数
	 */
	public function action_notes_rank2() {
		$model_note = new model_note();
		
		$res_all = $model_note->find('rank=2', '', '', 'COUNT(*) count');

		echo '目前有'.$res_all['count'].'条待办记事';
		exit();
	}
	
	/**
	 * 点击数
	 */
	public function action_link_hits() {
		$model_link_item = new model_link_item();
		$model_link_hits = new model_link_hits();
		
		$res_item = $model_link_item->find('1', '', '', 'SUM(hits) sum');
		
		return $this->output = $res_item['sum'].'-'.$model_link_hits->sum_today();
	}

	/**
	 * 天气
	 */
	public function action_weather() {
		$area = isset($_GET['area']) ? $_GET['area'] : 101010300;
		$result = library_common::get_contents('http://www.weather.com.cn/data/sk/'.$area.'.html', rand(1, 120));

		$result = json_decode($result);

		$get_time = round((time() - strtotime($result->weatherinfo->time)) / 60);	
	
		return $this->output = '<span title="'.$result->weatherinfo->city.' / '
			.$result->weatherinfo->WD.$result->weatherinfo->WS.' / '
			.$get_time.'分钟前'
			.'">'.$result->weatherinfo->temp.'℃</span>';
	}
	
	/**
	 * 新闻
	 */
	public function action_ifengnews() {
		$content = library_common::get_contents('http://news.ifeng.com/', rand(60, 1200));
		
		preg_match_all('/<h[12]>\s*<a href="(.*?)" target="_blank" class="sys_url">(.*?)</s', $content, $matches);

		// 整理
		$news = array();
		foreach ($matches[2] as $key => $value) {
			$value = trim($value);
			if($value != '') {
				$news[] = array(
					'title' => $value,
					'url' => isset($matches[1][$key]) ? $matches[1][$key] : '',
				);
			}
		}

		return $this->output = json_encode($news);
	}

    /**
     * 163滚动新闻
     */
    public function action_163news() {
        $content = file_get_contents('http://news.163.com/rollnews/'); 
        $content = iconv('gbk', 'utf-8', $content);

        preg_match('/<div class="list listBg1">(.*?)<\/div>/si', $content, $newsContent);

        $newsContent = isset($newsContent[1]) ? $newsContent[1] : '';

        preg_match_all('/<a href="(.*?)" target="_blank">(.*?)<\/a> <span>\((.*?)\)</', $newsContent, $newsArray);

        $newsResult = array();

        if (isset($newsArray[1]))
        {
            foreach ($newsArray[1] as $key => $item) 
            {
                $newsResult[] = array(
                    'url' => $item,
                    'title' => $newsArray[2][$key],
                    'time' => $newsArray[3][$key],
                );
            }
        }

        return $this->output = json_encode($newsResult);
    }

	/**
	 * 公用after
	 */
	public function after() {
		parent::after();
		
		// 显示结果的box
		$this->box_id = isset($_GET['box_id']) ? $_GET['box_id'] : '';
		
		// 回调函数
		$this->callback = isset($_GET['callback']) ? $_GET['callback'] : '';
		
		// 输出
		header('Content-type:text/html; charset=utf-8;');
		
		if($this->callback != '') {
			echo "{$this->callback}($this->output);";
		} elseif ($this->box_id != '') {
			echo "document.getElementById('{$this->box_id}').innerHTML = '{$this->output}';";
		} else {
			echo "document.write('{$this->output}');";
		}
	}
}
